#pragma once

namespace battle::object
{
    class Unit;
}

class dtNavMeshQuery;
class dtNavMesh;
namespace mmap
{

    class MMapData;
    class MMapMgr : public cftf::util::Singleton<MMapMgr>
    {
        friend class cftf::util::Singleton<MMapMgr>;
        MMapMgr() {}
    public:
        ~MMapMgr();

        bool LoadMap(unsigned int mapId, int x, int y);
        bool UnloadMap(unsigned int mapId, int x, int y);
        bool UnloadMap(unsigned int mapId);
        bool UnloadMapInstance(unsigned int mapId, unsigned int instanceId);

        // the returned [dtNavMeshQuery const*] is NOT threadsafe
        dtNavMeshQuery const* GetNavMeshQuery(unsigned int mapId, unsigned int instanceId);
        dtNavMesh const* GetNavMesh(unsigned int mapId);

        unsigned int GetLoadedTilesCount() const { return loaded_tiles_; }
        unsigned int GetLoadedMapsCount() const { return (unsigned int)loaded_mmaps_.size(); }

        void Clear();
        void PreventPathfindingOnMaps(const char* ignoreMapIds);
        bool IsPathfindingEnabled(unsigned int mapId, const battle::object::Unit* unit = nullptr);
        bool IsPathfindingForceEnabled(const battle::object::Unit* unit);
        bool IsPathfindingForceDisabled(const battle::object::Unit* unit);

    private:
        bool LoadMapData(unsigned int mapId);
        unsigned int PackTileID(int x, int y);

        std::unordered_map<unsigned int, MMapData*> loaded_mmaps_;
        unsigned int loaded_tiles_ = 0;
        std::shared_ptr<std::set<unsigned int>> disabled_ids_;
    };

}
